package cn.wqk.rrtf.service;

import cn.wqk.rrtf.mapper.ChatMapper;
import cn.wqk.rrtf.mapper.CourseMapper;
import cn.wqk.rrtf.mapper.ChatMapper;
import cn.wqk.rrtf.pojo.Chat;
import cn.wqk.rrtf.pojo.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ChatMapper chatMapper;

    @Override
    public Integer insertCourse(Course course, Chat chat) {
        try {
            courseMapper.insertCourse(course);
            Integer courseId = course.getCourseId();
            chat.setCourseId(courseId);
            chat.setName(course.getName());
            chat.setType(course.getType());
            chatMapper.insertChat(chat);
            return courseId;
        }catch (DataAccessException e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            System.out.println("添加公开课信息捕获异常："+e);
            return -1;
        }

    }

    @Override
    public Integer insertCourseByCourseIdAndMemberId(Integer courseId, Integer memberId) {
        int i=courseMapper.insertCourseByCourseIdAndMemberId(courseId,memberId);
        System.out.println(i+":报名公开课成功!");
        return i;
    }

    @Override
    public Integer deleteCourseByCourseId(Integer courseId) {
        int i=courseMapper.deleteCourseByCourseId(1);
        System.out.println(i+":删除公开课信息成功!");
        return i;
    }

    @Override
    public Integer updateCourse(Course course) {
        int i=courseMapper.updateCourse(course);
        System.out.println(i+":更新公开课信息成功!");
        return i;
    }

    @Override
    public List<Course> selectAllCourse() {
        List<Course> courseList = courseMapper.selectAllCourse();
        for (Course course : courseList) {
            System.out.println(course);
        }
        return courseList;
    }

    @Override
    public List<Course> selectAllCourseByStartTime() {
        List<Course> courseList = courseMapper.selectAllCourseByStartTime();
        for (Course course : courseList) {
            System.out.println(course);
        }
        return courseList;
    }

    @Override
    public List<Course> selectCourseByCondition(Course course) {
        List<Course> courseList = courseMapper.selectCourseByCondition(course);
        for (Course courses : courseList) {
            System.out.println(courses);
        }
        return courseList;
    }

    @Override
    public Course selectCourseByCourseId(Integer courseId) {
        Course course = courseMapper.selectCourseByCourseId(courseId);
        System.out.println(course);
        return course;
    }

    @Override
    public Course selectCourseByChatId(Integer chatId) {
        Course course = courseMapper.selectCourseByChatId(chatId);
        System.out.println(course);
        return course;
    }

    @Override
    public List<Course> selectCourseByMemberId(Integer memberId) {
        List<Course> courseList = courseMapper.selectCourseByMemberId(memberId);
        for (Course course : courseList) {
            System.out.println(course);
        }
        return courseList;
    }

    @Override
    public List<Course> selectCourseByTeacherId(Integer teacherId) {
        List<Course> courseList = courseMapper.selectCourseByTeacherId(teacherId);
        for (Course courses : courseList) {
            System.out.println(courses);
        }
        return courseList;
    }

}
